﻿using Api.WDS.Methods;
using Method.DAL;
using Models.operResult;
using Models.VModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Api.WDS.Operate
{
    public class UserDetailsOp
    {
        /// <summary>
        /// 获取用户信息由令牌
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public OperResult<V_UserInfo> GetUserInfoByToken(string token)
        {
            var uid = new GetUidByToken().GetUid(token);
            if (uid is null)
                return OperResult<V_UserInfo>.Failed("请重新登录");


            var data = new UserDetailsMet().GetUserInfo(Convert.ToInt32(uid));
            if (data is null)
                return OperResult<V_UserInfo>.Failed("该用户不存在");
            else
                return OperResult<V_UserInfo>.Succeed(data);
        }

        /// <summary>
        /// 修改昵称
        /// </summary>
        /// <param name="token"></param>
        /// <param name="newName"></param>
        /// <returns></returns>
        public OperResult UpdateNickName(string token, string newName)
        {
            var uid = new GetUidByToken().GetUid(token);
            if (uid is null)
                return OperResult.Failed("请重新登录");

            if (new UserDetailsMet().UpdateNickName(Convert.ToInt32(uid), newName))
                return OperResult.Succeed();
            return OperResult.Failed();
        }

        /// <summary>
        /// 获取用户关注列表
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public OperResult<List<V_UserPay>> GetUserPayList(string token)
        {
            var uid = new GetUidByToken().GetUid(token);
            if (uid is null)
                return OperResult<List<V_UserPay>>.Failed("请重新登录");

            var data = new UserDetailsMet().GetUserPayList(Convert.ToInt32(uid));
            return OperResult<List<V_UserPay>>.Succeed(data);
        }

        /// <summary>
        /// 获取用户关注列表
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public OperResult<List<V_UserPay>> GetUser_PayList(string token)
        {
            var uid = new GetUidByToken().GetUid(token);
            if (uid is null)
                return OperResult<List<V_UserPay>>.Failed("请重新登录");

            var data = new UserDetailsMet().GetUser_PayList(Convert.ToInt32(uid));
            return OperResult<List<V_UserPay>>.Succeed(data);
        }

        /// <summary>
        /// 用户关注/取消
        /// </summary>
        /// <returns></returns>
        public OperResult<bool> UserPayCheck(string token, string curUid)
        {
            var uid = new GetUidByToken().GetUid(token);
            if (uid is null)
                return OperResult<bool>.Failed("请重新登录");
            if (!int.TryParse(curUid, out int i))
                return OperResult<bool>.Failed("未查询到指定用户");

            switch (new UserDetailsMet().UserPayCheck(Convert.ToInt32(uid), Convert.ToInt32(curUid)))
            {
                case 1:
                    return OperResult<bool>.Succeed(true);
                case 2:
                    return OperResult<bool>.Succeed(false);
                default:
                    return OperResult<bool>.Failed("操作失败");
            }
        }

    }
}
